using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class IDManager {
	private Dictionary<NetworkViewID, int> nidToInt = new Dictionary<NetworkViewID, int>();
	private Dictionary<int, NetworkViewID> intToNid = new Dictionary<int, NetworkViewID>();
	private int nextId = 0;
	
	// Singleton pattern.
	private static IDManager _inst = null;
	public static IDManager inst() {
		if (_inst == null) {_inst = new IDManager();}
		return _inst;
	}
	
	// Private constructor.
	private IDManager() {}
	
	// Returns the id associated with the specified NetworkViewID.
	public int findByNid(NetworkViewID nid) {
		return nidToInt[nid];
	}
	
	// Returns the NetworkViewID associated with the specified id.
	public NetworkViewID findByID(int id) {
		return intToNid[id];
	}
	
	// Adds the specified NetworkViewID to the dictionary.
	public void add(int id, NetworkViewID nid) {
		nidToInt.Add (nid, id);
		intToNid.Add (id, nid);
	}
	
	// Clears all the dictionaries.
	public void clear() {
		nidToInt.Clear();
		intToNid.Clear();
	}
	
	// Should only be called by the server.
	public int add(NetworkViewID nid) {
		if (Network.isServer) {
			add(nextId, nid);
			nextId++;
		} else {Debug.LogError("Only the server may assign new ids!");}
		
		return nextId - 1;
	}
}
